<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/27
 * Time: 15:14
 */
namespace app\api\controller;
use think\Db;
class Notice extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //物业通知
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where1['xq_sh_status'] = 2;
        $where1['xq_user_id'] = $user_id;
        $result = Db::table('up_xq_property')->distinct(true)->where($where1)->column('xq_id');
        //查询物业通知
        $where['wy_xq_id'] = array('IN',$result);
        $info =Db::table('up_wy_notice')->alias('a')
            ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
            ->where($where)->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name')
            ->limit($limit,$num)->select();
        return json(['status' => 200, 'msg' =>$info]);
    }
    //我的小区
    public function myXq(){
        extract(input());
        $keyword = input('param.keyword');
        $where1['xq_sh_status'] = 2;
        $where1['xq_user_id'] = $user_id;
        $result = Db::table('up_xq_property')->distinct(true)->where($where1)->column('xq_id');
        $map['xq_id'] =array('IN',$result);
        $map['xq_name'] =array('like',"%$keyword%");
        $xq = Db::table('up_xq')->where($map)->select();
        foreach($xq as $k => $v){
            $xq[$k]['letter'] = $this->Getzimu($v['xq_name']);
        }
        $new_names = array_column($xq, 'letter');
        array_multisort($new_names, SORT_ASC, $xq);
        $start = '';
        $res = array();
        foreach ($xq as $k => $v) {
            if ($v['letter'] != $start) {
                $start = $v['letter'];
            }
            $res[$start][] = $v;
        }
        $jing = array();
        if(isset($res['#'])){
            $jing['#'] = $res['#'];
            unset($res['#']);
        }
        $xq = array_merge($res,$jing);
        return json(['status' => 200, 'msg' =>$xq]);
    }

    //搜索小区物业公告
    public function searchXq(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        if($id!=''){
            $where['wy_xq_id'] = $id;
        }else{
            $where2['xq_sh_status'] = 2;
            $where2['xq_user_id'] = $user_id;
            $result = Db::table('up_xq_property')->distinct(true)->where($where2)->column('xq_id');
            $where['wy_xq_id'] = array('IN',$result);
        }
        $info =Db::table('up_wy_notice')->alias('a')
            ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
            ->where($where)->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name')
            ->order('a.wy_time desc')
            ->limit($limit,$num)->select();
        return json(['status' => 200, 'msg' =>$info]);
    }
    //物业通知详情
    public function detailNotice(){
        extract(input());
        $id = input('param.id');
        $where['wy_id'] = $id;
        $info =Db::table('up_wy_notice')->alias('a')
            ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
            ->where($where)->field('a.wy_id,a.wy_title,a.wy_time,ux.xq_name,a.wy_content')
            ->find();
        return json(['status' => 200, 'msg' =>$info]);
    }

    /**
     * 消息列表
     * @return \think\response\Json
     * page  页码
     */
    public function systemNotice(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $type = input('param.type');
        if($type==2){//报事报修
            $where['n_classify'] = 1;
            $where['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            // 未被删除的
            $list = Db::table('up_notice')
                ->where($where)
                ->order('n_time desc')
                ->limit($limit,$num)
                ->select();
        }elseif ($type==3){//缴费
            $where['n_classify'] = 2;
            $where['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            // 未被删除的
            $list = Db::table('up_notice')
                ->where($where)
                ->order('n_time desc')
                ->limit($limit,$num)
                ->select();

        }elseif ($type==4){//系统消息
            $where['n_classify'] = 3;
            // 推送给我的
            $where['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            // 未被删除的
            $list = Db::table('up_notice')
                ->where($where)
                ->whereOr('n_type',1)
                ->order('n_time desc')
                ->limit($limit,$num)
                ->select();
        }elseif ($type==1){//全部消息
            $where['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            // 未被删除的
            $list = Db::table('up_notice')
                ->where($where)
                ->whereOr('n_type',1)
                ->order('n_time desc')
                ->limit($limit,$num)
                ->select();
        }

        if($list){
            foreach($list as $k => $v){
                // 已读/未读
                $read_user = explode(',', trim($v['n_read'], ','));
                if(in_array($user_id, $read_user)){
                    $list[$k]['is_read'] = 1; // 已读
                }else{
                    $list[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    public function haveNoticeRed()
    {
        extract(input());
        $data = Db::table('up_notice')
            ->where(function ($query) use ($user_id) {
                $query->where('n_type', 1)
                    ->where('n_classify','neq',2)
                    ->where('n_delete','not like',"%,$user_id,%")
                    ->where('n_read','not like',"%,$user_id,%");
            })
            ->whereOr(function ($query) use ($user_id) {
                $query
                    ->where('n_type', 2)
                    ->where('n_classify','neq',2)
                    ->where('n_user_id', 'like',"%,$user_id,%")
                    ->where('n_delete','not like',"%,$user_id,%")
                    ->where('n_read','not like',"%,$user_id,%");
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('n_type', 2)
                    ->where('n_classify','neq',2)
                    ->where('n_user_id', $user_id)
                    ->where('n_read',0);
            })
            ->find();
        $have = 0;
        if($data){
            $have = 1;
        }
        return json(['status'=>200,'msg'=>$have]);
    }
    /**
     * 系统消息详情 同时修改已读/未读状态
     * @return \think\response\Json
     * n_id   消息ID
     */
    public function noticeDetail(){
        extract(input());
        $n_id = input('n_id');
        $detail = Db::table('up_notice')
            ->field('n_title, n_content, n_read')
            ->where('n_id', $n_id)
            ->find();
        if($detail){
            // 是否是已读消息
            $is_read = explode(',', trim($detail['n_read'], ','));
            if(!in_array($user_id, $is_read)){ // 不在已读数组中，说明未读，需要加入已读字段
                $read_user = $detail['n_read'] .$user_id . ',';
                Db::table('up_notice')->where('n_id', $n_id)->setField('n_read', $read_user);
            }
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

}